/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.openmrs.module.simplelabentry.web.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Concept;
import org.openmrs.ConceptAnswer;
import org.openmrs.ConceptSet;
import org.openmrs.Privilege;
import org.openmrs.api.context.Context;
import org.openmrs.module.hospitalcore.BillingService;
import org.openmrs.module.hospitalcore.model.BillableService;
import org.openmrs.module.hospitalcore.model.Lab;
import org.openmrs.module.simplelabentry.SimpleLabEntryService;
import org.openmrs.module.simplelabentry.util.SimpleLabEntryConstants;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

public class FunctionalStatusController extends SimpleFormController {

    private static Log log = LogFactory.getLog(FunctionalStatusController.class);

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
    	SimpleLabEntryService ls = (SimpleLabEntryService) Context.getService(SimpleLabEntryService.class);
    	Lab lab = ls.getCurrentLab();
        Map<String, Object> map = new HashMap<String, Object>();
        //System.out.println("inside labtest constructor");
        BillingService bs =  Context.getService(BillingService.class);
        List<Concept> supportedLabConcepts = new ArrayList<Concept>(lab.getInvestigationsToDisplay());
        Map<Concept,Concept> allTestsMap = new HashMap<Concept,Concept>();
        ArrayList<Concept> allTests = new ArrayList<Concept>();
        for (int i = 0; i < supportedLabConcepts.size(); i++) {
            Collection<ConceptAnswer> answers = ((Concept) supportedLabConcepts.get(i)).getAnswers();
            for (ConceptAnswer concept : answers) {
                Concept c = concept.getAnswerConcept();
                if (c.getConceptClass().getName().equalsIgnoreCase("LabSet")) {
                    Collection<ConceptSet> cSets = c.getConceptSets();
                    if(!allTestsMap.containsKey(c))
                    {
                        allTestsMap.put(c, supportedLabConcepts.get(i));
                        allTests.add(c);
                    }
                    /*for (ConceptSet concept1 : cSets) {
                        //System.out.println("concept = "+concept1.getConcept().getName() + " conceptset = "+concept1.getConceptSet().getName());
                        Concept c1 = concept1.getConcept();
                        if (c1.getConceptClass().getName().equalsIgnoreCase("Test")) {
                            if(!allTestsMap.containsKey(c))
                            {
                                allTestsMap.put(c1, supportedLabConcepts.get(i));
                                allTests.add(c1);
                            }
                            //System.out.println("adding testname = " + c1.getName() + " id = " + c.getConceptId());
                        }
                    }*/
                } else {

                    if (c.getConceptClass().getName().equalsIgnoreCase("Test")) {
                        if(!allTestsMap.containsKey(c))
                        {
                            allTestsMap.put(c, supportedLabConcepts.get(i));
                            allTests.add(c);
                            //System.out.println("adding test id = " + c.getConceptId());
                        }
                    }
                }

            }
        }
        List<BillableService> newitems = new ArrayList();
        for (int i = 0; i < allTests.size(); i++) {
            BillableService ser = bs.getServiceByConceptId(allTests.get(i).getConceptId() );
            if(ser!=null)
            {
                newitems.add(ser);
            }
        }
        return newitems;
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
        Map functionalStatusModel = new HashMap();
        List<BillableService> labTests = (List<BillableService>) command;
        BillingService bs = (BillingService) Context.getService(BillingService.class);
        //List<Service> testItems = new ArrayList<Service>();
        for(BillableService s: labTests)
        {
        	BillableService s1 = bs.getServiceById(s.getServiceId());
            if(request.getParameter(s.getServiceId().toString())!=null && request.getParameter(s.getServiceId().toString()).equalsIgnoreCase("true"))
            {
                s1.setDisable(true);
                bs.saveService(s1);
            }
            else
            {
                //System.out.println("setting disabled false for service "+s.getName());
                s1.setDisable(false);
                bs.saveService(s1);
            }

        }
        //labTests.setItems(testItems);
        functionalStatusModel.put("labTests", labTests);
        return new ModelAndView(new RedirectView(request.getContextPath() + getSuccessView()), functionalStatusModel);
    }
}
